﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using RestSemitaService.Models;

namespace RestSemitaService.Controllers
{
    public class CustomerAddressController : ApiController
    {
        // GET api/customeraddress
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/customeraddress/5
        public string Get(string customer_id)
        {
            try
            {
                SqlConnection thisConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                thisConnection.Open();
                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = @" select address_text, address_gps from customer_address 
                where customer_id = @customer_id";
                thisCommand.Parameters.AddWithValue("@customer_id", customer_id);
                using (SqlDataReader reader = thisCommand.ExecuteReader())
                {
                    List<CustomerAddress> CustomerAddress = new List<CustomerAddress>();
                    while (reader.Read())
                    {
                        string _address_text = reader["address_text"].ToString();
                        string _address_gps = reader["address_gps"].ToString();
                        CustomerAddress.Add(new CustomerAddress
                        {
                            Address = _address_text,
                            Address_GPS = _address_gps
                        });
                    }
                    reader.Close();
                    thisConnection.Close();
                    string json = JsonConvert.SerializeObject(new
                    {
                        CustomerAddress = CustomerAddress
                    });
                    return json;
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return e.StackTrace + " \n >>>>>>>>>>>>>>>>> \n" + e.Message;
            }
        }

        // POST api/customeraddress
        // update address_gps for customer
        public string Post(string customer_id, string address_gps, string address_text)
        {
            try
            {
                SqlConnection thisConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                thisConnection.Open();
                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = @"update customer_address set 
                address_gps = @address_gps, 
                address_text = @address_text
                where customer_id = @customer_id";
                thisCommand.Parameters.AddWithValue("@customer_id", customer_id);
                thisCommand.Parameters.AddWithValue("@address_gps", address_gps);
                thisCommand.Parameters.AddWithValue("@address_text", address_text);
                if (thisCommand.ExecuteNonQuery() > 0)
                {
                    thisConnection.Close();
                    return "true";
                }
                else
                {
                    thisConnection.Close();
                    return "false";
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return e.StackTrace + " \n >>>>>>>>>>>>>>>>> \n" + e.Message;
            }
        }

        // PUT api/customeraddress/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/customeraddress/5
        public void Delete(int id)
        {
        }
    }
}
